<!-- f r o m 7 5  2 7 1  8 9 2 0 -->
<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item label="内部编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入内部编码" v-model="queryParam.contractCode"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入客户名称查询" v-model="queryParam.customerName"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select
                    v-model="queryParam.applicant"
                    allowClear
                    placeholder="选择申请人"
                    showSearch
                    optionFilterProp="children"
                  >
                    <a-select-option v-for="(item, index) in personList" :key="index" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-col :md="6" :sm="24">
                  <a-button type="primary" @click="searchQuery">查询</a-button>
                  <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
                </a-col>
              </span>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator" style="margin: 5px 0px">
          <a-button @click="myhandleAdd" type="primary" icon="plus">新增</a-button>
        </div>
        <!-- table区域-begin -->
        <div>
          <!--          :scroll="scroll"-->
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :scroll="{ x: 1600, y: 800 }"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="handleSee(record)">审核</a>
              <a-divider type="vertical" />
              <a @click="myhandleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => myhandleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
            <!-- 状态渲染模板 -->
            <template slot="customRenderFlag" slot-scope="enabled">
              <a-tag v-if="enabled" color="green">启用</a-tag>
              <a-tag v-if="!enabled" color="orange">禁用</a-tag>
            </template>
            <span slot="status" slot-scope="text, record">
              <a-tag :color="record.status == 2 ? 'orange' : record.status == 1 ? 'green' : 'yellow'">
                <div v-for="(item, index) in statusList" :key="index">
                  {{ record.status === item.id ? item.name : '' }}
                </div>
              </a-tag>
            </span>
            <span slot="approvalForm" slot-scope="text, record">
              <a @click="myhandleExportXls(record)">下载</a>
              <a-divider type="vertical" />
              <a @click="previewFile('报价记录表', record)">预览</a>
            </span>
            <span slot="applicant" slot-scope="text, record">
              <div v-for="(item, index) in personList" :key="index">
                {{ record.applicant == item.id ? item.name : '' }}
              </div>
            </span>
          </a-table>
        </div>
        <!-- table区域-end -->
        <!-- 表单区域 -->
        <SeeCustomerModal ref="modalFormSee" @ok="modalFormOk"></SeeCustomerModal>
        <ContractApprovalModal ref="ContractApproval" @ok="modalFormOk"></ContractApprovalModal>
      </a-card>
    </a-col>
  </a-row>
</template>
<!-- BY cao_yu_li -->
<script>
import SeeCustomerModal from './modules/SeeCustomerModal'
import ContractApprovalModal from './modules/ContractApprovalModal'
import { personList } from '@/api/api'
import { deleteAction, downFile, getActionImg } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { openDownloadDialog, sheet2blob } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
export default {
  name: 'CustomerList',
  mixins: [JeecgListMixin],
  components: {
    SeeCustomerModal,
    ContractApprovalModal,
    JDate
  },
  data() {
    return {
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      // 查询条件
      queryParam: {},
      personList: [],
      statusList: [
        {
          id: 1,
          name: '审批通过'
        },
        {
          id: 0,
          name: '待审批'
        },
        {
          id: 2,
          name: '驳回'
        }
      ],
      ipagination: {
        pageSizeOptions: ['10', '20', '30', '100', '200']
      },
      // 表头
      columns: [
        // {
        //   title: '#',
        //   dataIndex: '',
        //   key:'rowIndex',
        //   width:60,
        //   align:"center",
        //   customRender:function (t,r,index) {
        //     return parseInt(index)+1;
        //   }
        // },
        { title: '客户名称', key: '1', dataIndex: 'customerName', width: 150, align: 'center' },
        { title: '内部编码', key: '2', dataIndex: 'contractCode', width: 150, align: 'center' },
        { title: '合同编号', key: '3', dataIndex: 'contractNum', width: 150, align: 'center' },
        {
          title: '申请人',
          key: '4',
          dataIndex: 'applicant',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'applicant' }
        },
        { title: '审批流程', key: '5', dataIndex: 'stage', width: 150, align: 'center' },
        {
          title: '状态',
          key: '6',
          dataIndex: 'status',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '审批表',
          key: '7',
          dataIndex: 'approvalForm',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'approvalForm' }
        },

        {
          title: '操作',
          dataIndex: 'action',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '	/contractApproval/page',
        delete: '/contractApproval',
        deleteBatch: '/supplier/deleteBatch',
        importExcelUrl: '/supplier/importCustomer',
        exportXlsUrl: '/systemConfig/static',
        batchSetStatusUrl: '/supplier/batchSetStatus'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}${this.url.importExcelUrl}`
    }
  },
  mounted() {
    personList().then(res => {
      if (res.code == 200) {
        this.personList = res.data
        console.log(this.personList)
      } else {
        this.$message.warning(res.message)
      }
    })
  },
  methods: {
    previewFile(fileName, record) {
      if (record && record.approvalForm) {
        let params = record.approvalForm
        getActionImg('systemConfig/preview/' + params).then(res => {
          const binaryData = []
          binaryData.push(res)
          window.open(window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' })))
        })
      }
    },
    myhandleExportXls(record) {
      downFile(this.url.exportXlsUrl + '/' + record.approvalForm).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), record.approvalForm)
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', record.approvalForm)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    myhandleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete + '/' + id).then(res => {
        if (res.code === 200 || res.code === 10000) {
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    myhandleAdd: function() {
      this.$refs.ContractApproval.add()
      this.$refs.ContractApproval.title = '新增'
      this.$refs.ContractApproval.disableSubmit = false
    },
    searchReset() {
      this.queryParam = {
        type: '客户'
      }
      this.loadData(1)
    },
    handleSee: function(record) {
      this.$refs.modalFormSee.edit(record)
      this.$refs.modalFormSee.title = '审核'
      this.$refs.modalFormSee.disableSubmit = true
      if (this.btnEnableList.indexOf(1) === -1) {
        this.$refs.modalFormSee.isReadOnly = false
      }
    },
    myhandleEdit: function(record) {
      this.$refs.ContractApproval.edit(record)
      this.$refs.ContractApproval.title = '编辑'
      this.$refs.ContractApproval.disableSubmit = false
      if (this.btnEnableList.indexOf(1) === -1) {
        this.$refs.ContractApproval.isReadOnly = false
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
